package cn.dylan.pet.service.impl;

import cn.dylan.basic.exception.BusinessException;
import cn.dylan.basic.service.IBaiduAiAuditService;
import cn.dylan.basic.service.impl.BaseServiceImpl;
import cn.dylan.basic.util.*;
import cn.dylan.order.domain.OrderPetAcquisition;
import cn.dylan.order.mapper.OrderPetAcquisitionMapper;
import cn.dylan.org.domain.Employee;
import cn.dylan.org.domain.Shop;
import cn.dylan.org.mapper.EmployeeMapper;
import cn.dylan.org.mapper.ShopMapper;
import cn.dylan.pet.domain.Pet;
import cn.dylan.pet.domain.SearchMasterMsg;
import cn.dylan.pet.domain.SearchMasterMsgAuditLog;
import cn.dylan.pet.dto.AcceptDto;
import cn.dylan.pet.mapper.PetMapper;
import cn.dylan.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.dylan.pet.mapper.SearchMasterMsgMapper;
import cn.dylan.pet.query.SearchMasterMsgQuery;
import cn.dylan.pet.service.ISearchMasterMsgService;
import cn.dylan.user.domain.User;
import cn.dylan.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long id) {
        //先根据logininfoId查询出user对象 -- user_id 字段
        User user = userMapper.loadByLogininfoId(id);
        //user_id存入search
        searchMasterMsg.setUser_id(user.getId());

        searchMasterMsg.setNote("待审核");
        // 获取最近店铺
        Point point = DistanceUtils.getPoint(searchMasterMsg.getAddress());

        List<Shop> shops = shopMapper.loadAll();

        Shop nearestShop = DistanceUtils.getNearestShop(point, shops);

        searchMasterMsg.setShop_id(nearestShop.getId());

        searchMasterMsgMapper.save(searchMasterMsg);

        // 审核 人工智能审核
        Boolean textAudit = baiduAiAuditService.textAudit(searchMasterMsg.getName() + searchMasterMsg.getAddress()
                + searchMasterMsg.getNote() + searchMasterMsg.getTitle());
        Boolean imageAudit = baiduAiAuditService.imageAudit(searchMasterMsg.getResources());

        //日志对象
        SearchMasterMsgAuditLog smmlal = new SearchMasterMsgAuditLog();
        if(textAudit && imageAudit){
            // 审核通过 state设为1
            searchMasterMsg.setState(1);
            searchMasterMsgMapper.update(searchMasterMsg);// 修改没写
            smmlal.setMsg_id(searchMasterMsg.getId());
            smmlal.setState(1);
            smmlal.setAudit_time(new Date());
            smmlal.setNote("待处理");
            searchMasterMsgAuditLogMapper.save(smmlal);// 添加，没写
            // 短信、邮箱告诉用户

        }else {
            // 审核不通过，图片或text不合要求
            searchMasterMsg.setNote("上传信息有无，请核对后上传");
            searchMasterMsgMapper.update(searchMasterMsg);

            smmlal.setState(0);
            smmlal.setAudit_time(new Date());
            smmlal.setMsg_id(searchMasterMsg.getId());
            smmlal.setNote("审核不通过");
            searchMasterMsgAuditLogMapper.save(smmlal);
            throw new BusinessException("信息有误，核对后再上传");
        }
    }

    @Override
    public PageList<SearchMasterMsg> toHandle(SearchMasterMsgQuery query, Long id) {
        Employee emp = employeeMapper.loadByLogininfoId(id);
        if(emp.getShop_id()!=null){ // shop_id 不等于空  店铺管理人员
            query.setShopId(emp.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public void reject(Long id) {
        searchMasterMsgMapper.reject(id);
    }

    @Override
    public void accept(AcceptDto dto, Long id) {
        // 判断此订单是否被接单 处于处理中
        OrderPetAcquisition opa = orderPetAcquisitionMapper.loadByMsgId(dto.getMsgId());
        if(opa!=null){
            throw new BusinessException("该订单正在处理中");
        }

        /*
            处理需要生成一个pet对象   生成一个 订单对象
         */
        // 根据待处理信息生成pet对象
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.LoadById(dto.getMsgId()); // 方法没写
        Pet pet = searchMasterMsg2Pet(searchMasterMsg);

        petMapper.save(pet);//返回自增长id  存入订单需要

        // 根据pet,searchMasterMsg,dto生成一个订单对象
        OrderPetAcquisition opa1 = initOrderPetAcquisition(pet,searchMasterMsg,dto);
        // 保存到订单表里面
        orderPetAcquisitionMapper.save(opa1);
    }

    private OrderPetAcquisition initOrderPetAcquisition(Pet pet, SearchMasterMsg searchMasterMsg, AcceptDto dto) {
        OrderPetAcquisition opa = new OrderPetAcquisition();
        opa.setAddress(searchMasterMsg.getAddress());
        opa.setDigest(dto.getNote());
        // 订单过期事件为两天后
        Long lastcomfirmtimeLong = System.currentTimeMillis()+1000*60*60*24*2;
        opa.setLastcomfirmtime(new Date(lastcomfirmtimeLong));

        String orderSn = CodeGenerateUtils.generateOrderSn(dto.getHandler());
        opa.setOrderSn(orderSn);

        opa.setState(0);
        opa.setPrice(pet.getSaleprice());
        opa.setPet_id(pet.getId());

        /*user_id 购买人id*/
        opa.setShop_id(searchMasterMsg.getShop_id());

        opa.setEmp_id(dto.getHandler());

        opa.setSearch_master_msg_id(dto.getMsgId());
        return opa;
    }

    private Pet searchMasterMsg2Pet(SearchMasterMsg searchMasterMsg) {
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setResources(searchMasterMsg.getResources());
        pet.setShop_id(searchMasterMsg.getShop_id());
        pet.setType_id(searchMasterMsg.getPetType());
        pet.setSearch_master_msg_id(searchMasterMsg.getId());
        pet.setSaleprice(searchMasterMsg.getPrice());
        pet.setState(0);
        return pet;
    }


}
